#pragma once

#include "compile.hpp"
#include "runner.hpp"
#include "../common/log.hpp"
#include "../common/util.hpp"


#include <unistd.h>
#include <jsoncpp/json/json.h>



namespace rs_compile_and_run
{

    using namespace rs_util;
    using namespace rs_log;
    using namespace rs_compile;
    using namespace rs_runner;

    class CompileAndRun
    {
    public:
        static void RmTempFile(const std::string &file_name)
        {
            std::string _srcfile = PathUtil::SrcFile(file_name);
            if (FileUtil::IsFileExist(_srcfile))
                unlink(_srcfile.c_str());
            std::string _exefile = PathUtil::ExeFile(file_name);
            if (FileUtil::IsFileExist(_exefile))
                unlink(_exefile.c_str());
            std::string _errfile = PathUtil::ErrFile(file_name);
            if (FileUtil::IsFileExist(_errfile))
                unlink(_errfile.c_str());

            std::string _stdin = PathUtil::Stdin(file_name);
            if (FileUtil::IsFileExist(_stdin))
                unlink(_stdin.c_str());
            std::string _stdout = PathUtil::Stdout(file_name);
            if (FileUtil::IsFileExist(_stdout))
                unlink(_stdout.c_str());
            std::string _stderr = PathUtil::Stderr(file_name);
            if (FileUtil::IsFileExist(_stderr))
                unlink(_stderr.c_str());
        }

        static std::string CodeDesc(const std::string &file_name,int code)
        {
            std::string desc;
            switch ((code))
            {
            case 0:
                desc = "编译成功";
                break;
            case -1:
                desc = "提交代码为空";
                break;
            case -2:
                desc = "未知错误";
                break;
            case -3:
                //编译出错
                FileUtil::ReadFile(PathUtil::ErrFile(file_name),&desc,true);
                break;

            case SIGABRT:
                desc = "内存超出范围";
                break;
            case SIGXCPU:
                desc = "CPU超时";
                break;
            case SIGFPE:
                desc = "浮点数溢出";
                break;
            default:
                desc = "未知" + std::to_string(code);
                break;
            }
            return desc;
        }

        static void Start(const std::string& in_json,std::string* out_json)
        {
            Json::Value in_value;
            Json::Reader reader;
            reader.parse(in_json,in_value);
            
            std::string code = in_value["code"].asString();
            
            std::string input = in_value["input"].asString();
            
            
            int cpu_limit = in_value["cpu_limit"].asInt();
            
            int mem_limit = in_value["mem_limit"].asInt();
            
            int status_code = 0;
            Json::Value out_value;
            int run_result = 0;
            std::string file_name;
            
            if(code.size() == 0)
            {
                status_code = -1;
                goto END;
            }
            

            file_name = FileUtil::FileUniq();
            if(!FileUtil::WriteFile(PathUtil::SrcFile(file_name),code))
            {
                status_code = -2;//未知
                goto END;
            }

            if(!Compiler::Compile(file_name))
            {
                status_code = -3;
                goto END;
            }

            run_result = Runner::Run(file_name,cpu_limit,mem_limit);
            if(run_result < 0 )
            {
                status_code = -2;
            }
            else if(run_result > 0)
            {
                status_code = run_result;
            }
            else{
                status_code = 0;
            }
        END:
            out_value["status"] = status_code;
            out_value["reason"] = CodeDesc(file_name,status_code);

            if(status_code == 0)
            {
                std::string _stdout;
                FileUtil::ReadFile(PathUtil::Stdout(file_name),&_stdout,true);
                out_value["stdout"] = _stdout;

                std::string _stderr;
                FileUtil::ReadFile(PathUtil::Stderr(file_name),&_stderr,true);
                out_value["stderr"] = _stderr;
            }

            Json::StyledWriter writer;
            *out_json = writer.write(out_value);
            RmTempFile(file_name);
        }


        
    };

    

};